package com.hldsmart.smartda.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.activity.home.SearchActivity;
import com.hldsmart.smartda.adapter.home.ShoplvAdapter;
import com.hldsmart.smartda.model.entity.ShopFragmentEntity;
import com.hldsmart.smartda.utils.XListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunxiao on 2017/1/16.
 * 这是全部商家模块
 */
public class ShopFragment extends Fragment implements XListView.IXListViewListener,View.OnClickListener{
    private Spinner sp01,sp02;
    private XListView xlv_shop;
    private EditText search_et1;
    private List<ShopFragmentEntity> ll;
    private List<ShopFragmentEntity> list = new ArrayList<>();
    private ShoplvAdapter adapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int num = msg.what;
            if (num == 0) {
                if (adapter == null) {
                    adapter = new ShoplvAdapter(getActivity(), list);
                    xlv_shop.setAdapter(adapter);
                } else {
                    //刷新
                    adapter.notifyDataSetChanged();
                }
//            if (ll.size()<0){
//                adapter.notifyDataSetChanged();
//            }

            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //初始化布局及控件
        View view = inflater.inflate(R.layout.fragment_shop, container, false);

        initview(view);

        initShuJu();
        //初始化两个spinner
        initSpinner();


        return view;
    }

    //测试数据
    private void initShuJu() {
        ll = new ArrayList<>();
        ll.add(new ShopFragmentEntity("肯德基",R.drawable.ken,"美食  F3层"));
        ll.add(new ShopFragmentEntity("ONLY",R.drawable.xietupian,"服饰  F4层"));
        ll.add(new ShopFragmentEntity("星巴克",R.drawable.xin,"美食  F3层"));
        ll.add(new ShopFragmentEntity("罗宾汉",R.drawable.shopim,"服饰  F4层"));
        ll.add(new ShopFragmentEntity("奶茶嫁给粉",R.drawable.xin,"美食  F5层"));
        ll.add(new ShopFragmentEntity("UBZ女鞋",R.drawable.ubz,"服饰  F4层"));
        ll.add(new ShopFragmentEntity("班尼路服装",R.drawable.shopim,"服饰  F4层"));
        ll.add(new ShopFragmentEntity("特步女鞋",R.drawable.xietupian,"服饰  F4层"));
        ll.add(new ShopFragmentEntity("ONLY",R.drawable.ken,"美食  F4层"));
        ll.add(new ShopFragmentEntity("GAP",R.drawable.ubz,"服饰  F4层"));
        list.addAll(ll);

        handler.sendEmptyMessage(0);

    }

    private void initview(View view) {
        sp01 = (Spinner) view.findViewById(R.id.category_spn_spinner1);
        sp02 = (Spinner) view.findViewById(R.id.sort_spn_spinner2);
        search_et1 = (EditText) view.findViewById(R.id.search_et1);

        //上拉加载下拉刷新
        xlv_shop = (XListView) view.findViewById(R.id.xlv_shop);
        xlv_shop.setPullRefreshEnable(true);
        xlv_shop.setPullLoadEnable(true);

        //让Editext失去焦点，不会获取软键盘
        search_et1.setFocusable(false);
        search_et1.setOnClickListener(this);
        xlv_shop.setXListViewListener(this);
    }


    /**
     * 初始化spinner全部分类与智能排序
     */
    private void initSpinner() {
        //声明一个简单simpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), getListData(), R.layout.spn_item_style,
                new String[]{"npic", "namepic"}, new int[]{R.id.spn_iv_imageview, R.id.spn_tv_textview});

        //绑定到sp01
        sp01.setAdapter(simpleAdapter);
        //设置默认选项不弹出提示
        sp01.setSelection(0, true);
        sp01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //parent为一个Map结构的和数据
                Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);

                Toast.makeText(getContext(),
                        map.get("namepic").toString(), Toast.LENGTH_SHORT).show();

            }

            public void onNothingSelected(AdapterView<?> arg0) {


            }

        });
        //声明一个简单simpleAdapter
        SimpleAdapter simpleAdapter2 = new SimpleAdapter(getActivity().getBaseContext(), getListData2(), R.layout.spn_item_style,
                new String[]{"npic", "sort"}, new int[]{R.id.spn_iv_imageview, R.id.spn_tv_textview});

        // sp01.setPopupBackgroundDrawable(@);
        //绑定到sp02
        sp02.setAdapter(simpleAdapter2);
        //设置默认选项不弹出提示
        sp02.setSelection(0, true);
        sp02.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //parent为一个Map结构的和数据
                Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);

                Toast.makeText(getContext(),
                        map.get("sort").toString(), Toast.LENGTH_SHORT).show();

            }

            public void onNothingSelected(AdapterView<?> arg0) {


            }

        });

    }

    /**
     * 第1个spinner所需要的数据
     * @return
     */
    public List<Map<String, Object>> getListData() {

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        //每个Map结构为一条数据，key与Adapter中定义的String数组中定义的一一对应。

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("npic", R.mipmap.right);
        map.put("namepic", "全部分类");
        list.add(map);

        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("npic", R.mipmap.right);
        map2.put("namepic", "美食");
        list.add(map2);

        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("npic", R.mipmap.right);
        map3.put("namepic", "美妆");
        list.add(map3);

        HashMap<String, Object> map4 = new HashMap<String, Object>();
        map4.put("npic", R.mipmap.right);
        map4.put("namepic", "服饰");
        list.add(map4);

        HashMap<String, Object> map5 = new HashMap<String, Object>();
        map5.put("npic", R.mipmap.right);
        map5.put("namepic", "娱乐");
        list.add(map5);

        HashMap<String, Object> map6 = new HashMap<String, Object>();
        map6.put("npic", R.mipmap.right);
        map6.put("namepic", "超市");
        list.add(map6);



        return list;
    }
    /**
     * 第2个spinner所需要的数据
     * @return
     */
    public List<Map<String, Object>> getListData2() {

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        //每个Map结构为一条数据，key与Adapter中定义的String数组中定义的一一对应。

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("npic", R.mipmap.right);
        map.put("sort", "智能排序");
        list.add(map);

        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("npic", R.mipmap.right);
        map2.put("sort", "综合排序");
        list.add(map2);

        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("npic", R.mipmap.right);
        map3.put("sort", "距离最近");
        list.add(map3);

        HashMap<String, Object> map4 = new HashMap<String, Object>();
        map4.put("npic", R.mipmap.right);
        map4.put("sort", "人气最高");
        list.add(map4);

        HashMap<String, Object> map5 = new HashMap<String, Object>();
        map5.put("npic", R.mipmap.right);
        map5.put("sort", "活动最多");
        list.add(map5);

        return list;
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(
        new Runnable() {
            @Override
            public void run() {

                list.clear();
                //请求数据
                initShuJu();
                // 刷新适配器
                adapter.notifyDataSetChanged();
                xlv_shop.stopRefresh();

            }},2000);

    }

    @Override
    public void onLoadMore() {

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //请求数据
                initShuJu();
                // 刷新适配器
                adapter.notifyDataSetChanged();
                xlv_shop.stopLoadMore();
            }
        }, 2000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_et1:
                SearchActivity.startSearchActivity(getActivity());
                break;
        }
    }
}
